<template>
  <el-dialog
    v-el-drag-dialog
    title="添加全景图"
    :visible.sync="dialogFormVisible"
    :before-close="closeDialog"
    width="40%"
  >
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="temp"
      label-position="left"
      label-width="100px"
    >
      <el-form-item label="全景封面" prop="image">
        <upload-single-img
          :image-file-name="imageFileName"
          :temp="temp"
          :submit="submit"
          @upload-success="uploadSuccess"
        />
      </el-form-item>
      <el-form-item label="全景链接" prop="overallURL">
        <el-input v-model="temp.overallURL" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="addData">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import elDragDialog from '@/directive/el-drag-dialog'
import UploadSingleImg from '@/components/UploadSingleImg'
import { addPanorama } from '@/api/panorama'

export default {
  name: 'MyDialog',
  components: {
    UploadSingleImg
  },
  directives: { elDragDialog },
  props: {
    temp: {
      type: Object,
      required: true
    },
    dialogFormVisible: {
      type: Boolean,
      required: true
    },
    clickDialog: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      imageFileName: 'overallImage',
      submit: '',
      rules: {
        image: [
          {
            type: 'string',
            required: true,
            message: '请选择全景封面',
            trigger: 'change'
          }
        ],
        overallURL: [
          {
            type: 'string',
            required: true,
            message: '请选择全景链接',
            trigger: 'change'
          }
        ]
      }
    }
  },
  watch: {
    clickDialog() {
      this.$nextTick(() => {
        console.log(111)
        this.$refs['dataForm'].clearValidate()
      })
    }
  },
  methods: {
    addData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.submit = 'submit'
        }
      })
    },
    uploadSuccess() {
      addPanorama(this.temp)
        .then(response => {
          this.closeDialog()
          this.$emit('success')
          this.$notify({
            title: '提示',
            message: '添加成功！',
            type: 'success',
            duration: 2000
          })
        })
        .catch(error => {
          console.log(error)
        })
    },
    closeDialog() {
      this.$refs['dataForm'].clearValidate()
      this.$emit('close-dialog')
    }
  }
}
</script>

<style scoped>

</style>
